package app.com.itcast.biyesheji.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import app.com.itcast.biyesheji.Bean.ShopBean;
import app.com.itcast.biyesheji.R;

public class ShopActivity extends AppCompatActivity {

    private ImageView image;
    private TextView tv;
    private TextView tv2;
    private TextView tv3;
    private int id;
    private TextView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        initView();
        initData();
        initEvent();
    }

    private void initView() {
        image = findViewById(R.id.iv_1);
        tv = findViewById(R.id.tv_intro);
        tv2 = findViewById(R.id.tv_video);
        tv3 = findViewById(R.id.tv_chapter_intro);
        back = findViewById(R.id.tv_back);
    }

    private void initData() {
        Intent intent = getIntent();
        id = intent.getIntExtra("id",1);
        getData();
    }

    private void getData() {

        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        String url =getResources().getString(R.string.api_ip) + "/xiangqin/momo/findone";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", id);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.e("TAG", "onResponse: " + jsonObject);
                /*处理结果数据*/
                // 1）讲json对象转换为java对象（需要使用GsonFormat插件根据返回数据，创建结果处理类MyBean）
                Gson gson = new Gson();
                ShopBean shopBean = gson.fromJson(jsonObject.toString(), ShopBean.class);

                if(shopBean.isSuccess()){
                    // 获取数据，
                    ShopBean.DataBean dataBean = shopBean.getData();
                    // 在控件上显示文字
                    tv.setText(dataBean.getName());
                    tv2.setText(dataBean.getPrice()+"");
                    tv3.setText(dataBean.getText());
                    // 在控件上显示图片
                    Picasso.with(getApplicationContext())
                            .load(getResources().getString(R.string.api_ip)+dataBean.getImage())
                            .into(image);
                }else{
                    Toast.makeText(getApplicationContext(),  "失败", Toast.LENGTH_SHORT).show();
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
    }



    private void initEvent() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ShopActivity.this,ShouyeActivity.class);
                startActivity(intent);
            }
        });
    }
}